package JDBC;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

import Controler.TestDate;
import Model.Lieu;
import Model.Famille;
import Model.Eleve;


public class Eleve_Ajouter {


	public void ajouter(Eleve el) {

		try {	

			connexionBDD a = new connexionBDD();
			Connection conn=a.connect();

			Statement state = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

			Statement state1 = conn.createStatement();
			ResultSet result = state1.executeQuery("Select MAX(id_eleve) from eleve");
						
			int idel=0;
			
			try{
				while(result.next()){
					idel=Integer.parseInt(result.getObject(1).toString())+1;
				}
			}
			catch(Exception e){		
			}
			
			result.close();
			
			Statement state2 = conn.createStatement();
			ResultSet result2 = state2.executeQuery("Select MAX(id_lieu) from lieu");
			
			int idl=0;
			
			try{
				while(result2.next()){
						idl=Integer.parseInt(result2.getObject(1).toString())+1;
				}
			}
			catch(Exception e){
			}
			
			result2.close();
			
			Lieu l = el.getAdresse_cours();

			Statement state3 = conn.createStatement();
			ResultSet result3 = state3.executeQuery("Select MAX(id_famille) from famille");
			
			int idf=0;
			
			try{
				while(result3.next()){
						idf=Integer.parseInt(result3.getObject(1).toString())+1;
				}
			}
			catch(Exception e){
			}
			
			result2.close();
			
			Famille f = el.getId_famille();
			
			state.executeUpdate("INSERT INTO lieu VALUES ("+idl+","+l.numero+",'"+l.rue+"','"+l.complement+"',"+l.cp+",'"+l.ville+"',1);");
			System.out.println("Creation adresse reussie");
			state.executeUpdate("INSERT INTO famille VALUES("+idf+",'"+f.nom+"','"+f.prenom+"',"+f.num+",'"+f.mail+"',"+idl+");");
			System.out.println("Creation famille reussie");
			Date date =  TestDate.stringToDate("1990-12-11");
			System.out.println("Creation eleve");
			state.executeUpdate("INSERT INTO eleve VALUES ("+idel+",'"+el.nom+"','"+el.prenom+"','"+el.sexe+"','08-Jan-1979','"+el.email+"',"+el.tel+","+idl+","+idf+");");
			System.out.println("Creation eleve reussie");
			state.close();
		} 

		catch (Exception e) {
			e.printStackTrace();
		}	

	}

}
